import $urls from '@/utils/url.js'

function MultipleUpload(list = [], success) {
  let total = list.length;
  let count = 0;
  uni.showLoading({
    title: '上传中',
    mask: true
  })
  list.forEach((item, index) => {
    uni.uploadFile({
      url: $urls.upload,
      filePath: item,
      name: 'file',
      success(res) {
        count += 1;
        if (total === count) {
          uni.hideLoading();
        }
        if (res.statusCode === 200) {
          const data = JSON.parse((res.data));
          if (data.code === 20000) {
            success(data.data.url, total === count, index);
          } else {
            success('', total === count, index);
          }
        } else {
          success('', total === count, index);
        }
      },
      fail() {
        count += 1;
        if (total === count) {
          uni.hideLoading();
        }
        success('', total === count, index);
      }
    })
  })
}

module.exports = MultipleUpload;